package com.tiktok.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiktok.entity.Video;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 视频Mapper接口
 */
@Mapper
public interface VideoMapper extends BaseMapper<Video> {

    /**
     * 获取推荐视频列表（排除用户已观看的视频）
     */
    @Select("SELECT v.*, u.nickname, u.avatar " +
            "FROM video v " +
            "LEFT JOIN user u ON v.user_id = u.id " +
            "WHERE v.status = 1 " +
            "AND NOT EXISTS (SELECT 1 FROM user_video_view uvv WHERE uvv.user_id = #{userId} AND uvv.video_id = v.id) " +
            "ORDER BY v.like_count DESC, v.create_time DESC")
    List<Video> selectRecommendVideos(@Param("userId") Long userId, Page<Video> page);

    /**
     * 获取用户视频列表
     */
    @Select("SELECT v.*, u.nickname, u.avatar " +
            "FROM video v " +
            "LEFT JOIN user u ON v.user_id = u.id " +
            "WHERE v.user_id = #{userId} AND v.status = 1 " +
            "ORDER BY v.create_time DESC")
    List<Video> selectUserVideos(@Param("userId") Long userId, Page<Video> page);

    /**
     * 增加点赞数
     */
    @Update("UPDATE video SET like_count = like_count + 1 WHERE id = #{videoId}")
    int increaseLikeCount(@Param("videoId") Long videoId);

    /**
     * 减少点赞数
     */
    @Update("UPDATE video SET like_count = like_count - 1 WHERE id = #{videoId} AND like_count > 0")
    int decreaseLikeCount(@Param("videoId") Long videoId);

    /**
     * 增加播放数
     */
    @Update("UPDATE video SET view_count = view_count + 1 WHERE id = #{videoId}")
    int increaseViewCount(@Param("videoId") Long videoId);
} 